package com.yili.oa.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yili.oa.entity.SearchBase;
import com.yili.oa.entity.TableBase;
import com.yili.oa.models.SignIn;
import com.yili.oa.service.SignInService;
import com.yili.oa.utils.Exception.AjaxException;
import com.yili.oa.utils.ListUtils;
import com.yili.oa.vo.request.SignInReq;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lxr
 * @version v0.01
 * @date 2019/7/22 0022
 * @email 200890949@qq.com
 */
@Controller
@RequestMapping("signIn")
public class SignInApi {

    @Autowired
    private SignInService signInService;

    @GetMapping("/getList")
    @ResponseBody
    public TableBase getList(SearchBase search){
        PageHelper.startPage(search.getPage(),search.getLimit());
        List<SignIn> list = new ArrayList<>();
        list = signInService.getList(search);
        PageInfo pageInfo = new PageInfo<>(list,search.getLimit());
        TableBase tableBase = new TableBase();
        tableBase.setData(list);
        tableBase.setCode(0);
        tableBase.setMsg("");
        tableBase.setCount(pageInfo.getTotal());
        return tableBase;
    }

    @PostMapping("/add")
    @ResponseBody
    public String add(@RequestBody SignInReq signInReq){
        if (signInReq.getDay()==null){
            throw new AjaxException("请输入天数");
        }
        if (signInReq.getMoney()==null){
            throw new AjaxException("请输入奖励金额");
        }
        if (StringUtils.isBlank(signInReq.getId())){
            Boolean check = signInService.checkDay(signInReq.getDay(),null);
            if (!check){
                throw new AjaxException("该天数已存在");
            }
            signInService.add(signInReq);
        }else {
            Boolean check = signInService.checkDay(signInReq.getDay(),signInReq.getId());
            if (!check){
                throw new AjaxException("该天数已存在");
            }
            signInService.update(signInReq);
        }
        return "success";
    }

    /**
     * 跳转到添加页面
     * @return
     */
    @RequestMapping("/editButton/{signInId}")
    public String addButton(@PathVariable("signInId")String gameid, ModelMap modelAndView){
        SearchBase searchBase = new SearchBase();
        searchBase.setId(gameid);
        List<SignIn> list =signInService.getList(searchBase);
        SignIn signIn = new SignIn();
        if (ListUtils.isNotNullOrEmpty(list)){
            signIn=list.get(0);
        }
        modelAndView.addAttribute("info",signIn);
        return "game/addSignIn";
    }

    @RequestMapping("delete")
    @ResponseBody
    public Map<String,Object> deleteSignIn(@RequestBody List<String> list){
        Map<String, Object> map = new HashMap<>();
        signInService.delSignIn(list);
        map.put("result", "success");
        return map;
    }
}
